const CryptoJS = require('crypto-js');

// 基础编码函数（保持原样）
function tripletToBase64(e) {
    const c = "ZmserBbHoHQtNP+woOcza/LpngG8yJq42KWYj0DSfdikx3VT16IlUAFM97hECvuR5".split('');
    return c[e >> 18 & 63] + c[e >> 12 & 63] + c[e >> 6 & 63] + c[63 & e];
}

function encodeChunk(e, a, r) {
    const c = "ZmserBbHoHQtNP+woOcza/LpngG8yJq42KWYj0DSfdikx3VT16IlUAFM97hECvuR5".split('');
    let d = [];
    for (let f = a; f < r; f += 3) {
        const chunk = (e[f] << 16 & 0xff0000) + (e[f + 1] << 8 & 65280) + (255 & e[f + 2]);
        d.push(tripletToBase64(chunk));
    }
    return d.join("");
}

function b64Encode(e) {
    const c = "ZmserBbHoHQtNP+woOcza/LpngG8yJq42KWYj0DSfdikx3VT16IlUAFM97hECvuR5".split('');
    let r = e.length;
    let d = r % 3;
    let f = [];
    let s = 16383;
    let u = 0;
    let _ = r - d;

    for (; u < _; u += s) {
        f.push(encodeChunk(e, u, u + s > _ ? _ : u + s));
    }

    if (1 === d) {
        let a = e[r - 1];
        f.push(c[a >> 2] + c[a << 4 & 63] + "==");
    } else if (2 === d) {
        let a = (e[r - 2] << 8) + e[r - 1];
        f.push(c[a >> 10] + c[a >> 4 & 63] + c[a << 2 & 63] + "=");
    }

    return f.join("");
}

function encodeUtf8(e) {
    let a = encodeURIComponent(e);
    let r = [];
    for (let c = 0; c < a.length; c++) {
        let d = a.charAt(c);
        if ("%" === d) {
            let f = parseInt(a.charAt(c + 1) + a.charAt(c + 2), 16);
            r.push(f);
            c += 2;
        } else {
            r.push(d.charCodeAt(0));
        }
    }
    return r;
}

// 简化的 mnsv2 函数
function simpleMnsv2(data, md5Hash) {
    const timestamp = Date.now();
    // 创建一个简单的签名
    const simpleSign = CryptoJS.MD5(md5Hash + timestamp).toString();
    return simpleSign;
}

// 主要逻辑
function generateXS() {
    const c = '/api/sns/web/v1/homefeed{"cursor_score":"","num":31,"refresh_type":1,"note_index":10,"unread_begin_note_id":"","unread_end_note_id":"","unread_note_count":0,"category":"homefeed.fashion_v3","search_key":"","need_num":6,"image_formats":["jpg","webp","avif"],"need_filter_image":false}';

    // 计算 MD5
    const d = CryptoJS.MD5(c).toString();

    // 使用简化的 mnsv2
    const f = simpleMnsv2(c, d);

    const s = {
        x0: "4.2.1",
        x1: "xhs-pc-web",
        x2: "Windows",
        x3: f,
        x4: 'object'
    };

    const x_s = "XYS_" + b64Encode(encodeUtf8(JSON.stringify(s)));

    console.log("X_S_VALUE:" + x_s);
    return x_s;
}

// 执行并输出
generateXS();